// Utility routines for operations on Names.
package names

import (
	"path/filepath"
	"strings"
)

func IsAbsolute(name string) bool {
	return name[:1] == "/"
}

func HasPrefix(name string, prefix string) bool {
	return strings.HasPrefix(name, prefix)
}

func Clean(name string) string {
	// TODO(cnicolaou): we use this for now, but it's not clear
	// we even want to allow .. or . etc.
	return filepath.Clean(name)
}

func IsValid(name string) bool {
	return true
}

// PathPrefixes returns all possible prefixes with the longest one first.
// For example, for "/a/b/c" it will return ["/a/b/c", "/a/b", "/a"]
func PathPrefixes(name string) []string {
	c := strings.Split(name, "/")
	if len(c) <= 0 {
		return nil
	}
	r := make([]string, 0, len(c))
	for i := len(c); i >= 0; i-- {
		r = append(r, strings.Join(c[0:i], "/"))
	}
	return r
}

// TODO(cnicolaou)
// provide routines to break names into their paths, pull out subtrees,
// mountpoints, convert to/from urls. etc, etc
